package com.bigmi.admin.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigmi.admin.mapper.ChannelMapper;
import com.bigmi.admin.service.ChannelService;
import com.bigmi.model.admin.dto.ChannelDto;
import com.bigmi.model.common.dtos.PageResponseResult;
import com.bigmi.model.common.dtos.ResponseResult;
import com.bigmi.model.common.enums.AppHttpCodeEnum;
import com.bigmi.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, WmChannel> implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    //分页查询频道列表
    @Override
    public ResponseResult listPage(ChannelDto channelDto) {
        //判断参数是否为空
        if (channelDto ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        channelDto.checkParam();
        //获取登录人
        /*AdUser user = AdThreadLocalUtil.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"未登录");
        }*/
        IPage page=new Page(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper =new LambdaQueryWrapper<>();
        //模糊查询
        if (StringUtils.isNotBlank(channelDto.getName())){
            queryWrapper.like(WmChannel::getName,channelDto.getName());
        }
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page=page(page,queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        List records = page.getRecords();
        log.info(""+records);
        log.info(""+responseResult);
        return responseResult;
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel adChannel) {
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        System.out.println(adChannel);
        WmChannel wmChannel =new WmChannel();
        wmChannel.setName(adChannel.getName());
        wmChannel.setDescription(adChannel.getDescription());
        wmChannel.setIsDefault(true);
        wmChannel.setStatus(adChannel.getStatus());
        wmChannel.setOrd(adChannel.getOrd());
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        /*LambdaQueryWrapper<WmChannel> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getId,id);
        channelMapper.selectList(queryWrapper);*/
        channelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult upData(WmChannel wmChannel) {
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        System.out.println(wmChannel);
        WmChannel channel =new WmChannel();
        channel.setId(wmChannel.getId());
        channel.setName(wmChannel.getName());
        channel.setDescription(wmChannel.getDescription());
        channel.setIsDefault(wmChannel.getIsDefault());
        channel.setStatus(wmChannel.getStatus());
        channel.setOrd(wmChannel.getOrd());
        channel.setCreatedTime(new Date());
        channelMapper.updateById(channel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
